﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace LEVUtilityLibrary
{
    public class SecurityCryptography
    {
        public class AesCrypto
        {
            //const string _AesKEY = "Dynapack_Atd_Lev_Project_20140912";
            // The size of the KEY property must be the same as the BlockSize property divided by 8.
            const string _AesKEY = "Dynapack_Atd_Lev_Project20140912";
            // The size of the IV property must be the same as the BlockSize property divided by 8.
            const string _AesIV = "Dynapack_Atd_Lev";

            public static string EncryptBase64(string source)
            {
                AesCryptoServiceProvider aesc = new AesCryptoServiceProvider();
                //DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] key = Encoding.ASCII.GetBytes(_AesKEY);
                byte[] iv = Encoding.ASCII.GetBytes(_AesIV);
                byte[] dataByteArray = Encoding.UTF8.GetBytes(source);

                string encrypt = "";
                aesc.Key = key;
                aesc.IV = iv;
                try
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, aesc.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            cs.Write(dataByteArray, 0, dataByteArray.Length);
                            cs.FlushFinalBlock();
                            encrypt = Convert.ToBase64String(ms.ToArray());
                        }
                    }
                }
                catch (CryptographicException e)
                {
                    throw e;
                }
                catch (Exception e)
                {
                    throw e;
                }
                return encrypt;
            }

            public static string DecryptBase64(string encrypt)
            {
                AesCryptoServiceProvider aesc = new AesCryptoServiceProvider();
                //DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] key = Encoding.ASCII.GetBytes(_AesKEY);
                byte[] iv = Encoding.ASCII.GetBytes(_AesIV);
                try
                {
                    aesc.Key = key;
                    aesc.IV = iv;

                    byte[] dataByteArray = Convert.FromBase64String(encrypt);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, aesc.CreateDecryptor(), CryptoStreamMode.Write))
                        {
                            cs.Write(dataByteArray, 0, dataByteArray.Length);
                            cs.FlushFinalBlock();
                            return Encoding.UTF8.GetString(ms.ToArray());
                        }
                    }
                }
                catch (CryptographicException e)
                {
                    throw e;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }//public class AESCrypto

    }//public class SecurityCryptography
}//namespace LEVUtilityLibrary
